#statsNamespace.py
#Collects stats from files of namespace by startyear
#
#Health of Online Communities

import os
import collections
import sys
import multiprocessing

def main():
	
	inputDir = sys.argv[1]
	outputDir = sys.argv[2]

	inputFileList = os.listdir(inputDir)

	workers = []
	
	for inputFileName in inputFileList:
		print inputFileName
		
		inputFilePath = inputDir+'/'+inputFileName
		outputFilePath = outputDir+'/'+inputFileName
		
		if os.path.isfile(inputFilePath):

			worker = StatWorker(inputFilePath, outputFilePath)
			workers.append(worker)
			worker.start()

	for worker in workers:
		worker.join()

class StatWorker(multiprocessing.Process):

	def __init__(self, inputFilePath, outputFilePath):
		multiprocessing.Process.__init__(self)
		self.inputFilePath = inputFilePath
		self.outputFilePath = outputFilePath

	def run(self):
		stats(self.inputFilePath, self.outputFilePath)
		print "Finished statistics for " + self.inputFilePath + " in " + self.outputFilePath + "\n"
			
		
		
#prints out the facts
def stats(inputFilePath, outputFilePath):
	inputFileHandle = open(inputFilePath,"r")
	outputFileHandle = open(outputFilePath, 'a+')

	statDict = collections.defaultdict(int)
	outputFileHandle.write("Stats for " + inputFilePath)
	
	for line in inputFileHandle:
		pieces = line.split('\t')
		year = pieces[1].split('-')[0]
		startyear = pieces[3].split('-')[0]
		endyear = pieces[4].split('-')[0]
		tenure = int(endyear) - int(startyear)
		statDict[str(startyear)+str(int(year)-int(startyear))+str(tenure)] += 1 #edits by start year, offset year, and tenure SSSSOT
		statDict["TOTAL"] += 1 #all edits done in namespace by user who started in start year (lines in file)
	
	for y in sorted(statDict.keys()):
		outputFileHandle.write(str(y) + ' ' + str(statDict[y]) + '\n') #prints offset year followed by # of edits
	
	inputFileHandle.close()
	outputFileHandle.close()

main()
